﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Ymatou.CommonService;
using Ymatou.Evaluate.Entity;

namespace Ymatou.Evaluate.Common
{
    public static class BanWordUtil
    {
        /// <summary>
        /// 校验敏感词
        /// </summary>
        /// <param name="checkStr">需要校验的字符串</param>
        /// <param name="explain">是否显示详情</param>
        /// <param name="banWords">返回的敏感词列表</param>
        /// <returns>是否执行成功</returns>
        private static bool CheckBanWord(string checkStr, bool explain, out List<string> banWords)
        {
            banWords = new List<string>();
            try
            {
                if (string.IsNullOrEmpty(checkStr)) return false;
                var jsonStr = NetHelper.HttpPost(ServicesApiUrl.AntiSpamServiceUrl, new { sentence = checkStr, biz = "notecomment", explain = explain });
                if (string.IsNullOrEmpty(jsonStr)) return false;
                ApplicationLog.Info(string.Format("敏感词查询接口  返回:{0}", jsonStr));
                var model = JsonConvert.DeserializeObject<CheckBanWordResultEntity>(jsonStr);
                if (model != null && model.status == "OK")
                {
                    //填充返回的敏感词，并去除重复的组合。
                    if (model.hits.Count > 0) //原因：因为这是统一的接口,有些业务是返回多个结果的
                    {
                        foreach (var list in model.hits)
                        {
                            if (!list.spam)
                            {
                                continue;
                            }

                            foreach (var item in list.forbidden)
                            {
                                foreach (var itemsub in item.Value)
                                {
                                    if (!banWords.Contains(itemsub))
                                    {
                                        banWords.Add(itemsub);
                                    }
                                }
                            }
                        }
                    }
                }

                if (banWords == null || banWords.Count <= 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.Error("调用敏感词校验异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 评价敏感词处理
        /// </summary>
        /// <param name="checkStr">评价内容</param>
        /// <returns></returns>
        public static bool GetComments(ref string checkStr)
        {
            var banWords = new List<string>();
            var flag = CheckBanWord(checkStr, false, out banWords);
            if (flag && banWords != null)
            {
                string msg = checkStr;
                banWords.ForEach(x =>
                {
                    msg = msg.Replace(x, "**");
                });
                checkStr = msg;
                return true;
            }
            return false;
        }
    }
}
